/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.skyeye.modeling.customerized.part;

import org.eclipse.swt.accessibility.AccessibleEvent;
import org.skyeye.modeling.customerized.figures.ADInputFigure;
import org.skyeye.modeling.customerized.figures.BaseSwitchFigure;
import org.skyeye.modeling.customerized.figures.BulbInputFigure;
import org.skyeye.modeling.customerized.figures.InputFigure;
import org.skyeye.modeling.customerized.model.ADInput;
import org.skyeye.modeling.customerized.model.BaseSwitch;
import org.skyeye.modeling.customerized.model.BulbInput;
import org.skyeye.modeling.customerized.model.SimpleInput;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.AccessibleEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;

/**
 * EditPart for Output types in Logic Example
 */
public class InputEditPart extends LogicEditPart {

	protected AccessibleEditPart createAccessible() {
		return new AccessibleGraphicalEditPart() {

			public void getName(AccessibleEvent e) {
				e.result = getSimpleInput().toString();
			}

		};
	}

	/**
	 * Returns a newly created Figure.
	 */
	protected IFigure createFigure() {
		InputFigure figure;
		if (getModel() == null)
			return null;
		else if (getModel() instanceof BulbInput)
			figure = new BulbInputFigure(((BulbInput) getModel()).getID());
		else if (getModel() instanceof ADInput)
			figure = new ADInputFigure(((ADInput) getModel()).getID());
		else
			figure = new InputFigure();
		return figure;
	}

	/**
	 * Returns the model of this as a SimpleOutput.
	 * 
	 * @return Model of this as a SimpleOutput.
	 */
	protected SimpleInput getSimpleInput() {
		return (SimpleInput) getModel();
	}

	public void refreshVisuals() {
		getInputFigure().setValue(getInputModel().getResult());
		super.refreshVisuals();
	}

	public void propertyChange(java.beans.PropertyChangeEvent change) {
		if (change.getPropertyName().equals(SimpleInput.P_VALUE))
			refreshVisuals();
		else
			super.propertyChange(change);
	}

	public InputFigure getInputFigure() {
		return (InputFigure) getFigure();
	}

	protected SimpleInput getInputModel() {
		return (SimpleInput) getModel();
	}

	public void performRequest(Request request) {
		if (request.getType() == RequestConstants.REQ_OPEN) {
			System.out.println("SwitchEditPart performRequest");
			SimpleInput input = (SimpleInput) this.getModel();
			boolean current = input.getValue();
			if (current == true) {
				input.setValue(false);
			} else {
				input.setValue(true);
			}
			this.refreshVisuals();
		}
	}
}
